package com.TeacherInfoServer.common.service.Impl;

import com.TeacherInfoServer.pojo.DTO.PositionHistoryDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.PositionHistorySearchDTO;
import com.TeacherInfoServer.pojo.entity.Department;
import com.TeacherInfoServer.pojo.entity.PositionHistory;
import com.TeacherInfoServer.pojo.entity.Teacher;
import com.TeacherInfoServer.exception.DepartmentNameNotExistException;
import com.TeacherInfoServer.exception.RecordExistDetailException;
import com.TeacherInfoServer.exception.TeacherNameNotExistException;
import com.TeacherInfoServer.exception.TypeInfoNotExistException;
import com.TeacherInfoServer.mapper.PositionHistoryMapper;
import com.TeacherInfoServer.common.service.DepartmentService;
import com.TeacherInfoServer.common.service.PositionHistoryService;
import com.TeacherInfoServer.common.service.TeacherService;
import com.TeacherInfoServer.common.service.TypeInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PositionHistoryServiceImpl extends ServiceImpl<PositionHistoryMapper, PositionHistory> implements PositionHistoryService {
    @Autowired
    public PositionHistoryMapper positionHistoryMapper;

    @Autowired
    public TeacherService teacherService;

    @Autowired
    public DepartmentService departmentService;
    @Autowired
    public TypeInfoService typeInfoService;
    private final String typeInfo ="任职类型";

    @Override
    public Map<String,Object> getPositionList(Integer pages, Integer size){
        Page<PositionHistoryDTO> page = new Page<>(pages,size);
        QueryWrapper<PositionHistoryDTO> queryWrapper =new QueryWrapper<>();
        positionHistoryMapper.getPositionListByPage(page,queryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;
    }
    @Override
    public List<PositionHistoryDTO> getPositionHistoryDTOS(){
        return positionHistoryMapper.getPositionDTOs();
    }

    @Override
    public Map<String,Object> searchPositionHistoryList(PositionHistorySearchDTO positionHistorySearchDTO) {
        Page<PositionHistoryDTO> page = new Page<>(positionHistorySearchDTO.getPages(), positionHistorySearchDTO.getSize());
        positionHistoryMapper.searchPositionHistoryList(page, positionHistorySearchDTO);
        Map<String, Object> data = new HashMap<>();
        data.put("totals", page.getTotal());
        data.put("currentPages", page.getCurrent());
        data.put("pageNumber", page.getPages());
        data.put("rows", page.getRecords());
        return data;
    }
    @Transactional
    @Override
    public void addPositionHistory(String departmentName, String teacherName, String startDate, String endDate, String positionType){

        PositionHistory positionHistory = new PositionHistory();
        Department department = departmentService.findDepartmentByName(departmentName);
        if (department == null) {
            throw new DepartmentNameNotExistException();
        }
        Teacher teacher = teacherService.findTeacherByName(teacherName);
        if (teacher == null) {
            throw new TeacherNameNotExistException("教师不存在");
        }
        if(typeInfoService.getOne("任职类型",positionType)==null){
            throw  new TypeInfoNotExistException("任职类型不存在");
        }
        QueryWrapper<PositionHistory> queryWrapper = new QueryWrapper<>();
        if(positionHistoryMapper.selectOne(queryWrapper.eq("department_id",department.getId())
                .eq("start_date",startDate)
                .eq("end_date",endDate)
                .eq("teacher_id",teacher.getTeacherId())
                .eq("position_type",typeInfoService.getOneId("任职类型",positionType))) == null) {
            positionHistory.setDepartmentId(department.getId());
            positionHistory.setTeacherId(teacher.getTeacherId());
            positionHistory.setPositionType(typeInfoService.getOneId("任职类型",positionType));
            positionHistory.setStartDate(startDate);
            positionHistory.setEndDate(endDate);
            positionHistoryMapper.insert(positionHistory);
        }
        else {
            throw new RecordExistDetailException("已有完全相同记录");
        }
    }
    @Transactional
    @Override
    public void addPositionHistoryByDTO(PositionHistoryDTO positionHistoryDTO){
        Department department = departmentService.findDepartmentByName(positionHistoryDTO.getDepartmentName());
        if (department == null) {
            throw new DepartmentNameNotExistException();
        }
        PositionHistory positionHistory = new PositionHistory();
        Teacher teacher = teacherService.findTeacherByName(positionHistoryDTO.getTeacherName());
        if (teacher == null) {
            throw new TeacherNameNotExistException("未找到任职人");
        }
        if(typeInfoService.getOne("任职类型",positionHistoryDTO.getPositionType())==null){
            throw  new TypeInfoNotExistException("任职类型不存在");
        }
        QueryWrapper<PositionHistory> queryWrapper = new QueryWrapper<>();
        if(positionHistoryMapper.selectOne(queryWrapper.eq("department_id",department.getId())
                .eq("start_date",positionHistoryDTO.getStartDate())
                .eq("end_date",positionHistoryDTO.getEndDate())
                .eq("teacher_id",teacher.getTeacherId())
                .eq("position_type",typeInfoService.getOneId("任职类型",positionHistoryDTO.getPositionType()))) == null) {
            positionHistory.setDepartmentId(department.getId());
            positionHistory.setTeacherId(teacher.getTeacherId());
            positionHistory.setPositionType(typeInfoService.getOneId("任职类型",positionHistoryDTO.getPositionType()));
            positionHistory.setStartDate(positionHistoryDTO.getStartDate());
            positionHistory.setEndDate(positionHistoryDTO.getEndDate());
            positionHistoryMapper.insert(positionHistory);
        }else{
            throw new RecordExistDetailException("已有完全相同记录");
        }
    }

    @Transactional
    @Override
    public void updatePositionHistory(PositionHistoryDTO positionHistoryDTO){
        Teacher teacher = teacherService.findTeacherByName(positionHistoryDTO.getTeacherName());
        if (teacher == null) {
            throw new TeacherNameNotExistException("未找到任职人");
        }
        Department department = departmentService.findDepartmentByName(positionHistoryDTO.getDepartmentName());
        if (department == null) {
            throw new DepartmentNameNotExistException();
        }
        QueryWrapper<PositionHistory> queryWrapper = new QueryWrapper<>();
        if(positionHistoryMapper.selectOne(queryWrapper.eq("id",positionHistoryDTO.getId())
                .eq("department_id",department.getId())
                .eq("start_date",positionHistoryDTO.getStartDate())
                .eq("end_date",positionHistoryDTO.getEndDate())
                .eq("teacher_id",teacher.getTeacherId())
                .eq("position_type",typeInfoService.getOneId("任职类型",positionHistoryDTO.getPositionType())))!=null ) {

            throw new RecordExistDetailException("未进行修改");
        }
        QueryWrapper<PositionHistory> queryWrapper1 = new QueryWrapper<>();
        if(!positionHistoryMapper.selectList(queryWrapper1
                .eq("department_id",department.getId())
                .eq("start_date",positionHistoryDTO.getStartDate())
                .eq("end_date",positionHistoryDTO.getEndDate())
                .eq("teacher_id",teacher.getTeacherId())
                .eq("position_type",typeInfoService.getOneId("任职类型",positionHistoryDTO.getPositionType()))).isEmpty()) {

            throw new RecordExistDetailException("已有完全相同记录");
        }
            UpdateWrapper<PositionHistory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", positionHistoryDTO.getId())
                    .set("department_id", department.getId())
                    .set("start_date", positionHistoryDTO.getStartDate())
                    .set("end_date", positionHistoryDTO.getEndDate())
                    .set("teacher_id", teacher.getTeacherId())
                    .set("position_type", typeInfoService.getOneId("任职类型",positionHistoryDTO.getPositionType()));
            positionHistoryMapper.update(null,updateWrapper);
    }
    @Transactional
    @Override
    public boolean deletePositionHistory(List<Long> ids){
        if (ids == null || ids.isEmpty()) {
            return false;
        }

        QueryWrapper<PositionHistory> queryWrapper= new QueryWrapper<>();
        queryWrapper.in("id",ids);
        int deleteCount=positionHistoryMapper.delete(queryWrapper);
        return deleteCount>0;
    }




}
